package com.tanhua.backstage.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.backstage.service.DashboardService;
import com.tanhua.backstage.vo.AddUsersVo;
import com.tanhua.backstage.vo.DistributionVo;
import com.tanhua.backstage.vo.SummaryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@Slf4j
@RequestMapping("dashboard")
public class DashboardController {

    @Autowired
    private DashboardService dashboardService;

    /**
     * 注册用户分布，行业top、年龄、性别、地区
     * @return
     */
    @GetMapping("distribution")
    public ResponseEntity<DistributionVo> distribution(@RequestParam("sd") Long startDate,
                                                       @RequestParam("ed") Long endDate){


        try {

            DistributionVo distributionVo=  this.dashboardService.distribution(startDate,endDate);

            //对distributionVo进行非空判断
            if (ObjectUtil.isNotEmpty(distributionVo)) {

                //如果不是空,将distributionVo返回给前端
                return ResponseEntity.ok(distributionVo);
            }
        } catch (Exception e) {
            log.error("查询注册用户分布，行业top、年龄、性别、地区信息出错了..."+e);
        }

        //返回500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();


    }


    /**
     * 新增、活跃用户、次日留存率
     * @return
     */
    @GetMapping("users")
    public ResponseEntity<AddUsersVo> AddUserData(@RequestParam("sd") Long startDate,
                                                  @RequestParam("ed") Long endDate,
                                                   @RequestParam("type")Integer type){
        try {

            AddUsersVo addUsersVo=  this.dashboardService.AddUserData(startDate,endDate,type);

            //对distributionVo进行非空判断
            if (ObjectUtil.isNotEmpty(addUsersVo)) {

                //如果不是空,将distributionVo返回给前端
                return ResponseEntity.ok(addUsersVo);
            }
        } catch (Exception e) {
            log.error("查询新增、活跃用户、次日留存率信息出错..."+e);
        }

        //返回500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();


    }


    /**
     * 概要统计信息
     * @return
     */
    @GetMapping("summary")
    public ResponseEntity<SummaryVo> summary(){

        try {
            SummaryVo summaryVo=this.dashboardService.summary();

            //对返回的summaryVo对象进行非空判断
            if (ObjectUtil.isNotEmpty(summaryVo)) {
                //不是空,返回summaryVo对象给前端
                return ResponseEntity.ok(summaryVo);
            }
        } catch (Exception e) {
            log.error("查询概要统计信息出错..."+e);
        }

        //返回500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


}
